package de.hbrs.ooka.crs.servlet;

import de.hbrs.ooka.beans.PaperBeanStatelessLocal;
import de.hbrs.ooka.beans.UserSessionStatefulLocal;
import de.hbrs.ooka.crs.servlet.constants.WebPageNames;
import de.hbrs.ooka.entities.PaperEntity;
import de.hbrs.ooka.entities.UserEntity;
import de.hbrs.ooka.enums.Role;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ReviewerViewServlet extends AbstractViewServlet {

    @EJB
    private PaperBeanStatelessLocal paperBean;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        UserSessionStatefulLocal crsBean = this.getCrsBean(request);
        String redirection = "";
        RequestDispatcher rd = null;

        if (crsBean == null || !this.isUserAuthenticated(crsBean.getUser(), Role.REVIEWER)) {
            request.setAttribute("message", "Not logged in");
            request.setAttribute("redirect", WebPageNames.LoginViewPage);
            rd = request.getRequestDispatcher("/" + WebPageNames.ErrorPage + ".jsp");
            if (rd != null) {
                rd.forward(request, response);
                return;
            }
        } else {
            String action = request.getParameter("action");
            if (action == null) {

                List<PaperEntity> paperReviewer = new ArrayList<PaperEntity>();

                for (PaperEntity paperEntity : this.paperBean.getAllPapers()) {
                    if (paperEntity.getReviewer().contains(crsBean.getUser())) {
                        paperReviewer.add(paperEntity);
                    }
                }

                request.setAttribute("name", crsBean.getUser().getName());
                request.setAttribute("roles", crsBean.getUser().getRoles());
                request.setAttribute("paperList", paperReviewer);

                rd = request.getRequestDispatcher("/" + WebPageNames.ReviewerViewPage + ".jsp");
                if (rd != null) {
                    rd.forward(request, response);
                    return;
                }
            } else if (request.getParameter("action").equalsIgnoreCase("review")) {
                String paper = request.getParameter("paperlist");
                paper = (paper == null) ? "Paper0.pdf" : paper;
                String grade = request.getParameter("grade");
                String textDescription = request.getParameter("report");
                String passed = request.getParameter("passed");

                redirection = this.executeInputString(crsBean.getUser(), paper, grade, passed, textDescription);
                System.out.println("redirect to: " + redirection);
                request.setAttribute("redirect", WebPageNames.ReviewerViewPage);
                if(redirection.equalsIgnoreCase(WebPageNames.ErrorPage)){
                    request.setAttribute("message", "Rating of paper " + paper + " failed");
                } else {
                    request.setAttribute("message", "Paper " + paper + " successfully rated");
                }
                rd = request.getRequestDispatcher("/" + redirection);
                if (rd != null) {
                    rd.forward(request, response);
                    return;
                }
            }

            response.sendRedirect(redirection);
        }
    }

    private String executeInputString(UserEntity reviewer, String paper, String grade, String passed, String textDescription) {
        try {
            if (textDescription == null || textDescription.length() <= 0) {
                return WebPageNames.ErrorPage;
            }

            int iGrade = Integer.parseInt(grade);
            boolean bPassed = Boolean.parseBoolean(passed);
            return this.executeInput(reviewer, paper, iGrade, bPassed, textDescription);
        } catch (Throwable e) {
            return WebPageNames.ErrorPage + ".jsp";
        }
    }

    private String executeInput(UserEntity reviewer, String paper, int grade, boolean passed, String textDescription) {
        this.paperBean.reviewPaper(reviewer, paper, grade, passed, textDescription);
        return WebPageNames.ActionSuccessfulPage;
    }
}
